package com.ifcar.app.weibo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;

import net.oauth.OAuthAccessor;
import net.oauth.OAuth.Parameter;

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

public class PostEntityMultiPart extends PostEntity{

    /**
     * @param accessor
     * @param oauthInfo
     */
    public PostEntityMultiPart(OAuthAccessor accessor, Properties oauthInfo) {
        super(accessor, oauthInfo);
    }

    /**
     * @see example.PostEntity#genEntity(java.util.Properties)
     */
    @Override
    protected HttpEntity genEntity(Properties postInfo) throws Exception {
        MultipartEntity entity =
            new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        if (postInfo != null) {
            for(Entry<Object, Object> entry : postInfo.entrySet()) {
                //File
                if(File.class.isInstance(entry.getValue())) {
                    entity.addPart((String) entry.getKey(),
                            new FileBody((File)entry.getValue()));
                }
                //String
                else {
                    entity.addPart((String) entry.getKey(), 
                            new StringBody(toString(entry.getValue())));
                }
            }
        }
        return entity;
    }

    /**
     * @see example.PostEntity#getSignProperties(java.lang.String, java.util.Properties)
     */
    @Override
    protected List<Parameter> getSignProperties(String url, Properties postInfo) {
        List<Parameter> list = new ArrayList<Parameter>();
        for (Entry<Object, Object> entry: this.oauthInfo.entrySet()) {
            list.add(new Parameter((String) entry.getKey(), toString(entry
                    .getValue())));
        }
        return list;
    }

}
